
package exception.thrower;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.CommonHttpServlet;

/**
 * url:<br>
 * 1.http://127.0.0.1:8080/JSP/exception/thrower?name=grady.kao, <br>
 * 2.http://127.0.0.1:8080/JSP/exception/thrower?name=grace, 模擬發生 ThrowerErrorException<br>
 * 3.http://127.0.0.1:8080/JSP/exception/thrower, 模擬發生 ThrowerNullException<br>
 * 
 * <br>
 * 1. 有值(grady.kao), 將會導到 info.jsp, 並顯示名字<br>
 * 2. url parameter 的 name 值 錯誤, 就產生並丟出 ThrowerErrorException, 並依 <error-page> 的設定導到 nameError.jsp<br>
 * 3. url parameter 的 name 為 null, 就產生並丟出 ThrowerNullException, 並依 <error-page> 的設定導到 nameNull.jsp,<br>
 * 但是輸出沒有超過 512 bytes(整個 jsp 的容量大小),就會顯示 IE 製定的內容(假設 "IE/進階/顯示易懂的HTTP錯誤訊訊息" 這個是啟動的)
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class ThrowerExceptionServlet extends CommonHttpServlet
{
	private static final long serialVersionUID = 4238182768572731701L;

	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		String name = request.getParameter( "name" );

		if( name == null )
		{
			throw new ThrowerNullException( "姓名 is null" );
		}
		else if( !name.equals( "grady.kao" ) )
		{
			throw new ThrowerErrorException( "姓名 is error" );
		}
		else
		{
			request.setAttribute( "Name", name );
			request.getRequestDispatcher( "/exception/info.jsp" ).forward( request, response );
		}
	}
}
